package com.zzty.train.pjm.controller;


import com.zzty.train.pjm.entity.User;
import com.zzty.train.pjm.service.UserService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.mapdb.Caches;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "/checkUser")
    public Map<String,String> checkUser(User user){
        String result="true";
        System.out.println("111111111"+user.toString());
        String newPassword = new SimpleHash("md5", user.getPassword(),  ByteSource.Util.bytes(user.getPassword()), 2).toHex();
        user.setPassword(newPassword);
        User user1 = userService.getUserByName(user);
        Map<String,String>map=new HashMap<>();
        if(null==user1){
            map.put("result","no");//查询不到用户
            result="false";
        }
        if("true".equals(result)&&user.getPassword().equals(user1.getPassword())){
            map.put("result",String.valueOf(user1.getType()));//密码正确，返回用户类型
        }
        if("true".equals(result)&&!user.getPassword().equals(user1.getPassword())){
            map.put("result","wrong secret");//密码错误
        }
        System.out.println("user"+user.getPassword());

        return map;
    }
}
